package grpc

import (
	"context"
	"fmt"
	"gitlab.local/backend/proto/dto"
	"gitlab.local/backend/proto/manager"
	"manager/bll"
	log "manager/collector/logger"
	"manager/errors"
	"manager/model"
	"manager/utils"
)

type Ebkie struct {
}

// GetEbikeInfoById 获取车辆信息
func (e *Ebkie) GetEbikeInfo(ctx context.Context, in *manager.EbikeInfoReq, out *manager.EbikeInfoRsp) (err error) {
	fmt.Println("---- grpc GetEbikeInfoById data:", utils.ToJsonString(in))
	// 查找车辆信息
	info, err := bll.Ebike.Info(ctx, &model.EbikeBaseRequest{ID: in.BikeId, SnCode: in.SnCode})
	if err != nil {
		log.Errorf("EBikeService GetEbikeInfoById err:%v", err)
		return
	}

	// 构造回包
	*out = manager.EbikeInfoRsp{BikeInfo: &manager.BikeInfo{
		BikeId:    info.ID,
		Name:      info.Name,
		Status:    int32(info.Status),
		SnCode:    info.SNCode,
		Color:     info.Color,
		IsBind:    int32(info.IsBind),
		Stock:     int32(info.Stock),
		StoreId:   int32(info.StoreID),
		CreatedAt: info.CreatedAt,
	}}
	return
}

func (e *Ebkie) GetEbikeTypeList(ctx context.Context, in *manager.GetEbikeTypeListReq, out *manager.GetEbikeTypeListRsp) (err error) {

	fmt.Println("---- grpc GetEbikeTypeList data:", utils.ToJsonString(in))
	rsp, err := bll.EbikeType.ParentList(ctx, &model.ParentEbikeTypeRequest{
		Index: int(in.Index),
		Size:  int(in.Size),
		Name:  in.BikeTypeName,
		IDs:   in.BikeTypeIds,
	})
	if err != nil {
		log.Errorf("EBikeService GetEbikeTypeList err:%v", err)
		return
	}
	*out = manager.GetEbikeTypeListRsp{
		Total: int32(len(rsp)),
	}

	for i := 0; i < len(rsp); i++ {
		out.List = append(out.List, &manager.BikeTypeInfo{
			Id:   rsp[i].ID,
			Name: rsp[i].Name,
		})
	}
	return
}

// GetEbikeList 获取车辆信息
func (e *Ebkie) GetEbikeList(ctx context.Context, in *manager.GetEbikeListReq, out *manager.GetEbikeListRsp) (err error) {
	fmt.Println("---- grpc GetEbikeList data:", utils.ToJsonString(in))
	rsp, err := bll.Ebike.List(ctx, &model.EbikeListRequest{
		Index:     int(in.Index),
		Size:      int(in.Size),
		SNCodes:   in.SnCodes,
		BikeIds:   in.BikeIds,
		TypeIds:   in.BikeTypeIds,
		TypeName:  &in.BikeTypeName,
		TypeNames: in.BikeTypeNames,
		Statuses:  in.Status,
		UserIds:   in.UserIds,
	})
	if err != nil {
		log.Errorf("EBikeService GetEbikeList err:%v", err)
		return
	}

	*out = manager.GetEbikeListRsp{
		Total: int32(rsp.Total),
	}
	//
	//for i := 0; i < len(rsp.Data); i++ {
	//	out.List = append(out.List, &manager.BikeInfo{
	//		BikeId:    rsp.Data[i].ID,
	//		Name:      rsp.Data[i].DeviceTypeName,
	//		Status:    int32(rsp.Data[i].Status),
	//		SnCode:    "",
	//		Color:     "",
	//		IsBind:    int32(rsp.Data[i].IsBind),
	//		Stock:     0,
	//		StoreId:   int32(rsp.Data[i].StoreID),
	//		CreatedAt: rsp.Data[i].CreatedAt,
	//	})
	//}

	for i := 0; i < len(rsp.Data); i++ {
		out.List = append(out.List, &manager.BikeInfo{
			UserId:       rsp.Data[i].UserID,
			UserName:     rsp.Data[i].UserName,
			BikeId:       rsp.Data[i].ID,
			BikeTypeId:   rsp.Data[i].TypeID,
			BikeTypeName: rsp.Data[i].DeviceTypeName,
			Name:         rsp.Data[i].Name,
			Status:       int32(rsp.Data[i].Status),
			SnCode:       rsp.Data[i].SNCode,
			Color:        "",
			IsBind:       int32(rsp.Data[i].IsBind),
			Stock:        0,
			StoreId:      int32(rsp.Data[i].StoreID),
			CreatedAt:    rsp.Data[i].CreatedAt,
		})
	}
	return
}

// GetUserEbikeList 获取用户车辆列表
func (e *Ebkie) GetUserEbikeList(ctx context.Context, in *manager.GetUserEbikeListReq, out *manager.GetUserEbikeListRsp) (err error) {
	fmt.Println("---- grpc GetUserEbikeList data:", utils.ToJsonString(in))
	list, err := bll.Ebike.AppList(ctx, in.UserId)
	if err != nil {
		log.Errorf("EBikeService GetUserEbikeList err:%v", err)
		return
	}

	*out = manager.GetUserEbikeListRsp{}
	out.Total = int32(len(list))
	for i := 0; i < len(list); i++ {
		out.List = append(out.List, &manager.BikeInfo{
			BikeId:    list[i].ID,
			Name:      list[i].Name,
			Status:    0,
			SnCode:    list[i].SNCode,
			Color:     list[i].Color,
			IsBind:    0,
			Stock:     0,
			StoreId:   0,
			CreatedAt: list[i].CreatedAt,
		})
	}
	return
}

func (e *Ebkie) DeviceCountByUser(ctx context.Context, in *dto.CountDeviceByIdsRequest, out *dto.CountDeviceByIdsInfoResponse) error {
	fmt.Println("---- grpc DeviceCountByUser data:", utils.ToJsonString(in))
	var (
		err        error
		ebikeCount = make([]*model.FindCountByIdsResponse, 0)
		d          = &dto.CountDeviceByIdsInfoResponse{}
	)
	if in == nil {
		return errors.EmptyParameter.Error()
	}

	ebikeCount, err = bll.UserEbike.FindCountByIds(ctx, in.Ids)
	if err != nil {
		return err
	}

	for _, response := range ebikeCount {
		d.DeviceCounts = append(d.DeviceCounts, &dto.CountDeviceByIdsInfo{
			Id:    response.ID,
			Count: response.Count,
		})
	}

	*out = *d
	return err
}
